package app.engine.solucoes.request.command;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import app.engine.solucoes.request.beans.Pergunta;
import app.engine.solucoes.request.beans.Usuario;
import app.engine.solucoes.request.dao.AppDAO;
import app.engine.solucoes.request.exception.CommandException;
public class GetAllCommand implements Command {
	private String next;
	public GetAllCommand(String next) {
		this.next = next;
	}
	
	public String execute(HttpServletRequest req) throws CommandException {
		try {
			AppDAO dao = new AppDAO();
			Usuario usuario = (Usuario) req.getSession().getAttribute("usuario");
			List<Object> perguntas  = (List<Object>) dao.selectAll(Pergunta.class);
			
			for (Iterator<Object> iterator = perguntas.iterator(); iterator.hasNext();) {
				Pergunta pergunta = (Pergunta) iterator.next();
				if (!pergunta.getEmpresa().equals(usuario.getEmpresa())) {
					iterator.remove();
				}
			}
			req.setAttribute("perguntas", perguntas);
			return next;
		} catch (Exception fe) {
			throw new CommandException("GetCommand: " + fe.getMessage());
		}
		
	}
}